package lumis.content.control.button;

import java.util.Collection;

import lumis.doui.control.button.ButtonControl;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinterface.ServiceInterfaceConfig;
import lumis.util.XmlUtil;
import lumis.util.log.ILogger;
import lumis.util.log.LoggerFactory;

/**
 * Responsible for calling the process action handler with id="commitAndPreview".
 * <p>
 * Generally included in the edit popup interfaces
 * <p>
 * The process action handler responses usually refresh the parent window, maintain
 * the current window open and opens another preview window passing the currently 
 * editing content to be previewed.
 * <p>
 * @version $Revision: 8618 $ $Date: 2007-12-18 11:34:20 -0200 (Tue, 18 Dec 2007) $
 * @since 4.1.0
 */
public class SaveAndPreviewButtonControl extends ButtonControl
{
	private static ILogger logger = LoggerFactory.getLogger(PreviewButtonControl.class);
	
	public void setRenderData() throws PortalException
	{
		// detect if SaveAndPreview button should be available
		String serviceId = douiContext.getRequest().getServiceConfig().getId();
		
		boolean isPreviewSupported = false;
		try
		{
			Collection<String> ServiceInterfaceIds = ManagerFactory.getServiceInterfaceManager().getIdsByServiceId(serviceId, douiContext.getTransaction());
			for(String serviceInterfaceId: ServiceInterfaceIds)
			{
				ServiceInterfaceConfig serviceInterfaceConfig = ManagerFactory.getServiceInterfaceManager().getServiceInterfaceConfig(serviceInterfaceId,  douiContext.getTransaction());
				if(serviceInterfaceConfig.isPreviewSupported())
				{
					isPreviewSupported = true;
					break;
				}
			}
		}
		catch (UnexpectedException e) 
		{
			throw e;
		}
		
		// if no interface Id is specified, remove the added button
		if(!isPreviewSupported)
		{
			logger.debug("No preview supported interface found. Removing the control.");

        	parentControl.removeChild(this);
			return;
		}
		
		String processActionId;

		if(XmlUtil.selectSingleNode("onClick", controlDefinitionNode) == null)
		{
			processActionId = XmlUtil.readAttributeString("processActionId", controlDefinitionNode, "commitAndPreview");

			StringBuilder onClickXml = new StringBuilder();
			onClickXml.append(XmlUtil.XML_HEADER+"<onClick type=\"processAction\">");
			onClickXml.append("<processActionId>"+processActionId+"</processActionId>");		
			onClickXml.append("</onClick>");

			XmlUtil.addNodeFromXmlString(controlDefinitionNode, onClickXml.toString());
		}

		if(XmlUtil.selectSingleNode("image", controlDefinitionNode) == null)
		{
			XmlUtil.addTextNode(controlDefinitionNode, "image", "lumis/portal/client/images/SaveAndPreview.gif");
		}

		if(XmlUtil.readAttributeString("text", controlDefinitionNode) == null)
		{
			XmlUtil.addNodeAttr(controlDefinitionNode, "text", "STR_SAVE_AND_PREVIEW");
		}
		
		super.setRenderData();
	}
}
